<!--
 * @Author: tangchuan 1944302118@qq.com
 * @Date: 2024-02-27 16:22:42
 * @LastEditors: tangchuan 1944302118@qq.com
 * @LastEditTime: 2024-03-08 11:18:13
 * @FilePath: \jeecg-novel\src\views\novel\bookIndex\components\BookIndexModal.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <book-index-setting-form v-if="isSet" ref="settingForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false" />
    <BookIndexForm v-else ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false" />
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose } from 'vue';
  import BookIndexForm from './BookIndexForm.vue';
  import BookIndexSettingForm from './BookIndexSettingForm.vue';

  const title = ref<string>('');
  const width = ref<number>(800);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const isSet = ref<boolean>(false);
  const registerForm = ref();
  const settingForm = ref();
  const emit = defineEmits(['register', 'success']);

  /**
   * 新增
   */
  function add() {
    title.value = '新增';
    visible.value = true;
    isSet.value = false;
    nextTick(() => {
      registerForm.value.add();
    });
  }

  /**
   * 编辑
   * @param record
   */
  function edit(record) {
    isSet.value = false;
    title.value = disableSubmit.value ? '详情' : '编辑';
    visible.value = true;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }

  /**
   * 批量设置收费章节
   */
  function set() {
    isSet.value = true;
    title.value = '批量设置收费章节';
    visible.value = true;
    nextTick(() => {
      settingForm.value.set();
    });
  }

  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    if (isSet.value) {
      settingForm.value.submitForm();
    } else {
      registerForm.value.submitForm();
    }
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
    if (isSet.value) {
      settingForm.value.cancelForm();
    } else {
      registerForm.value.cancelForm();
    }
  }

  defineExpose({
    add,
    edit,
    set,
    disableSubmit,
  });
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
